package io.renren.modules.skate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.common.constant.Constant;
import io.renren.common.utils.ConvertUtils;
import io.renren.modules.nuaa.entity.TCertificateEntity;
import io.renren.modules.skate.dao.FCertificateDao;
import io.renren.modules.skate.dao.FClassifyDao;
import io.renren.modules.skate.dao.FCompetitionDao;
import io.renren.modules.skate.dto.FCertificateDTO;
import io.renren.modules.skate.entity.FCertificateEntity;
import io.renren.modules.skate.entity.FClassifyEntity;
import io.renren.modules.skate.entity.FCompetitionEntity;
import io.renren.modules.skate.service.FCertificateService;
import io.renren.modules.security.user.SecurityUser;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 证书表
 *
 * @author Mark sunlightcs@gmail.com
 * @since 3.0 2023-11-28
 */
@Service
public class FCertificateServiceImpl extends CrudServiceImpl<FCertificateDao, FCertificateEntity, FCertificateDTO> implements FCertificateService {

    @Autowired
    private FClassifyDao fClassifyDao;
    @Autowired
    private FCompetitionDao fCompetitionDao;

    @Override
    public QueryWrapper<FCertificateEntity> getWrapper(Map<String, Object> params){
        QueryWrapper<FCertificateEntity> wrapper = new QueryWrapper<>();

        String type = (String)params.get("type");
        if(StringUtils.isNotBlank(type)){
            wrapper.eq("type",Integer.parseInt(type));
        }

        return wrapper;
    }


    @Override
    public FCertificateDTO queryScore(Map<String, Object> params) {
        String name = (String)params.get("name");
        String type = (String)params.get("type");
        String idCard = (String)params.get("idCard");
        QueryWrapper<FCertificateEntity> wrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(type)){
            wrapper.eq("type",type);
        }
        wrapper.eq("name",name).eq("id_card",idCard).last("limit 1");

        FCertificateEntity fCertificateEntity = baseDao.selectOne(wrapper);

        return ConvertUtils.sourceToTarget(fCertificateEntity, currentDtoClass());
    }

    @Override
    public List<FCertificateDTO> queryByIdCardAndType(String idCard, int i) {
        List<FCertificateEntity> list = baseDao.selectList(new QueryWrapper<FCertificateEntity>().eq("id_card", idCard).eq("type", 1));
        if(i == 1 && list != null){
            list.forEach(dto ->{
                FClassifyEntity fClassifyEntity = fClassifyDao.selectById(dto.getClassifyId());
                if(fClassifyEntity != null){
                    dto.setClassifyName(fClassifyEntity.getClassifyName());
                }
            });
        }else if(i == 2 && list != null){
            list.forEach(dto ->{
                FCompetitionEntity fCompetitionEntity = fCompetitionDao.selectById(dto.getMatchId());
                if(fCompetitionEntity != null){
                    dto.setMatchName(fCompetitionEntity.getName());
                }
            });
        }
        return ConvertUtils.sourceToTarget(list, currentDtoClass());
    }

    public List<FCertificateEntity> getByIdCard(String idCard, String type) {
        return this.baseDao.selectList(
                new QueryWrapper<FCertificateEntity>()
                        .eq("id_card", idCard)
                        .eq("type", type)
        );
    }

}